package com.functest.orm

import com.functest.framework.BaseFunctionalTest
import com.functest.orm.TestData.Student
import com.functest.orm.TestData.Student_withLongId
import org.testng.annotations.Test

/**
 * Created with IntelliJ IDEA.
 * User: MalyutinAA
 * Date: 04.07.13
 * Time: 18:19
 * To change this template use File | Settings | File Templates.
 */
class BaseProviderByIdTest extends BaseFunctionalTest {
    @Test
    void testByIdPositive() {
        asTest {
            TestData.Student student
            asStep( 'byId() when value is negative int'){
                 student = Student.provider.byId(-1)
                 assert student.firstName == 'Petr'
                 assert student.lastName == 'Petrov'
                 assert student.status == TestData.StudentStatus.Student
            }

            asStep( 'byId() with null found field'){
                student = Student.provider.byId(5)
                assert student.firstName == 'Alexandr'
                assert student.lastName == null
                assert student.status == TestData.StudentStatus.Expelled
            }

            asStep( 'byId() when value is 0'){
                student = Student.provider.byId(0)
                assert student.firstName == 'Maria'
                assert student.lastName == 'Petrova'
                assert student.status == TestData.StudentStatus.Student
            }

            asStep( 'byId() when value is correct'){
                student = Student.provider.byId(2)
                assert student.firstName == 'Ivan'
                assert student.lastName == 'Ivanov'
                assert student.status == TestData.StudentStatus.Graduate
            }

            asStep('byId with String with correct numerical value'){
                String value = "2"
                student = Student.provider.byId(value)
                assert student.firstName == 'Ivan'
                assert student.lastName == 'Ivanov'
                assert student.status == TestData.StudentStatus.Graduate
            }

            asStep('byId with big int value'){
                TestData.Student_withLongId studentWitLongId

                TestData.Student_withLongId sidorova = Student_withLongId.provider.byFieldValueFirst('firstName', 'Julia')
                Long _id = sidorova.id
                studentWitLongId = Student_withLongId.provider.byId(_id)

                assert studentWitLongId.firstName == "Julia"
                assert studentWitLongId.lastName == "Sidorova"
                assert studentWitLongId.status == TestData.StudentStatus.Graduate
            }
        }
    }

    @Test
    void testByIdNegative(){
        asTest {
            TestData.Student student
            asStep('byId() with nonexistent int value'){
                student = Student.provider.byId(10)
                assert student == null
            }

            asStep('byId with String value'){
                String value = "val"
                student = Student.provider.byId(value)
                assert student == null
            }

            asStep('byId with null value'){
                def value = null
                try {
                    Student.provider.byId(value)
                }
                catch (Throwable e){
                    assert e instanceof AssertionError
                }
            }
        }

        //SQL Lite defines key id type
        //..add test for BigDecimal id
    }


}



